package dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dao.OrderDao;
import dao.util.DBConnection;
import po.Order;
import po.OrderDetail;

public class OrderDaoImpl implements OrderDao{
	Connection con = null;
	PreparedStatement ps = null;
	@Override
	public int addOrder(Order o, int cId) {
		ResultSet rs = null;
		con = DBConnection.connect();
		String sql = "insert into c_order(oId,time,sumPrice,totalNum,cId) values(?,?,?,?,?)";		
		int m = 0;
		try {
			ps = con.prepareStatement(sql);
			ps.setString(1, o.getoId());
			ps.setDate(2,(java.sql.Date)o.getoDate());
			ps.setDouble(3, o.getSumPrice());
			ps.setInt(4, o.getSumNum());
			ps.setInt(5, cId);
			m = ps.executeUpdate();
			return m;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBConnection.free(con, ps, rs);
		}
		
		return 0;
	}

	@Override
	public int delOrder(String oId) {
		ResultSet rs = null;
		con = DBConnection.connect();
		String sql = "delete from c_orders where oId=?";		
		int m = 0;
		try {
			ps = con.prepareStatement(sql);
			ps.setString(1, oId);
			m = ps.executeUpdate();
			if(m>0){
				return 1;	
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBConnection.free(con, ps, rs);
		}
		
		return 0;
	}

	@Override
	public List<Order> queryOrder(int cId) {
		ResultSet rs = null;
		con = DBConnection.connect();
		String sql = "select * from c_orders where cId=?";
		try {
			ps = con.prepareStatement(sql);
			ps.setInt(1, cId);
			rs = ps.executeQuery();
			List<Order> orderList = new ArrayList<Order>();
			while(rs.next()){
				String oId = rs.getString("oId");
				Date oDate = rs.getDate("time");
				Double sumPrice = rs.getDouble("sumPrice");
				int sumNum = rs.getInt("totalNum");
				Order order = new Order(oId, sumPrice, sumNum, oDate);
				orderList.add(order);
				
			}
			return orderList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBConnection.free(con, ps, rs);
		}
		
		return null;
	}

	@Override
	public List<OrderDetail> queryOrderDish(String oId) {
		ResultSet rs = null;
		con = DBConnection.connect();
		String sql = "select * from orders_dihes where oId=?";
		try {
			ps = con.prepareStatement(sql);
			ps.setString(1, oId);
			rs = ps.executeQuery();
			List<OrderDetail> orderList = new ArrayList<OrderDetail>();
			while(rs.next()){
				int dId = rs.getInt("dId");
				int num = rs.getInt("num");
				String odId = rs.getString("odId");
				OrderDetail orderDetail = new OrderDetail(odId, num, dId);
				orderList.add(orderDetail);
				
			}
			return orderList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBConnection.free(con, ps, rs);
		}
		
		return null;
	}

	@Override
	public int addOrderDish(Order o, int oId) {
		ResultSet rs = null;
		con = DBConnection.connect();
		String sql = "insert into orders_dishes(odId,num,dId,oId) values(?,?,?,?)";		
		int m = 0;
		try {
			ps = con.prepareStatement(sql);
			ps.setString(1, o.getOdId());
			ps.setInt(2,o.getNum());
			ps.setDouble(3, o.getdId());
			ps.setString(4, o.getoId());
			m = ps.executeUpdate();
			return m;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBConnection.free(con, ps, rs);
		}
		
		return 0;
	}

	@Override
	public int delOrderDish(String oId) {
		ResultSet rs = null;
		con = DBConnection.connect();
		String sql = "delete from c_orders where oId=?";		
		int m = 0;
		try {
			ps = con.prepareStatement(sql);
			ps.setString(1, oId);
			m = ps.executeUpdate();
			if(m>0){
				return 1;	
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBConnection.free(con, ps, rs);
		}
		
		return 0;
	}

}
